﻿using System;
using System.Linq;
using HiTeachers.Entities;
using HiTeachers.Services;
using HiTeachers.Services.Exceptions;

namespace HiTeachers.Web
{
    public partial class StudentFeedback : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.CloseMessage(lblMsg);
            Page.CloseMessage(lblFeedbackErr);

            if (Page.IsPostBack) return;

            var ctdReqId = new Guid(Request["ctreqid"]);
            ContactedRequest contactedRequest = ContactedRequestServices.GetContactedRequest(ctdReqId);
            if (contactedRequest == null)
            {
                divFeedback.Visible = false;
                Page.DisplayMessage(lblFeedbackErr, "Bạn chưa được cấp quyền để gửi phản hồi.", MessageType.Error);

                return;
            }

            teacherPic.ImageUrl = contactedRequest.Teacher.ImagePath;
            ltrName.Text = contactedRequest.Teacher.Name;

            string subTitles = string.Empty;
            string[] subjects = contactedRequest.Teacher.Subject.Split(',');
            subTitles = subjects.Select(subject => SubjectServices.GetById(Convert.ToInt16(subject))).Aggregate(subTitles, (current, sub) => current + (sub.Name + ", "));
            if (!string.IsNullOrEmpty(subTitles)) subTitles = subTitles.Substring(0, subTitles.Length - 2);

            ltrSubject.Text = subTitles;
            ltrAddress.Text = string.Format("{0} ,{1}, {2}", contactedRequest.Teacher.Address,
                                            contactedRequest.Teacher.District, contactedRequest.Teacher.City);
            ltrEmail.Text = contactedRequest.Teacher.Email;
            ltrPhone.Text = contactedRequest.Teacher.PhoneNumber;
        }

        protected void BtnSendFeedbackClick(object sender, EventArgs e)
        {
            try
            {
                captCode.ValidateCaptcha(txtCaptText.Text);
                if (!captCode.UserValidated)
                {
                    lblMsgCapt.Visible = true;
                    return;
                }

                var ctdReqId = new Guid(Request["ctreqid"]);
                var contactedRequest = ContactedRequestServices.GetContactedRequest(ctdReqId);
                var studentFeedback = new Entities.StudentFeedback
                {
                    Content = txtFeedback.Text,
                    Rate = feedbackRating.CurrentRating,
                    Email = contactedRequest.Request.Email,
                    Name = contactedRequest.Request.Name,
                    Teacher = contactedRequest.Teacher,
                    IsReviewed = true
                    
                };

                StudentFeedbackServices.SaveOrUpdate(studentFeedback);


                Teacher teacher = studentFeedback.Teacher;
                teacher.Rate = StudentFeedbackServices.GetAvgRatingOnTeacher(teacher.ID);
                TeacherServices.SaveOrUpdate(teacher);

                Page.DisplayMessage(lblMsg, "Ý kiến đánh giá của bạn đã được gửi đi.", MessageType.Success);
                txtFeedback.Text = string.Empty;
            }
            catch (ObjectAlreadyExistException)
            {
                Page.DisplayMessage(lblMsg, "Bạn đã gửi ý kiến đánh giá cho giáo viên rồi!", MessageType.Error);
            }
        }

        protected void BtnCancelClick(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }
}
